package com.sjl.file;


import cn.hutool.core.util.StrUtil;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Stream;


public class RedFileUtil {
    public static final String FILE_NAME = "/Users/sjl/fsdownload/wx-pay.2020-05-20.0.log";

    public static void main(String[] args) {

        try {
            Stream<String> lines = Files.lines(Paths.get(FILE_NAME));
            List<String> list = readFileNIO(lines);
            getMe(lines,list);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * JDK8 NIO读取文件
     *
     * @return
     * @throws Exception
     */
    public static List<String> readFileNIO(Stream<String> lines) throws Exception {
        List<String> list = new LinkedList();
        List<String> uuidList = new LinkedList();
        lines.forEach(line -> {
            String split = line.trim();// .trim()可以去掉首尾多余的空格

            String tmpStr = "";
            if (split.contains("=end=")) {
                int start = split.indexOf("耗时:");
                int end = split.indexOf("毫秒");
                String sum = split.substring(start + 3, end);

                if (Integer.parseInt(sum) > 700) {
                    list.add(sum + ">" + split);
                    tmpStr = split.substring(1, 33);
                    uuidList.add(tmpStr);
                    System.out.println(sum + ">" + split);
                }
            }
        });
        System.out.println(list.size());
        return uuidList;
    }

    public static void getMe(Stream<String> stream, List<String> uuids) throws Exception {

        Set<Map<String,String>> setMe = new HashSet<>(100);
        Map<String,String> map = new HashMap<>(1);
        Files.lines(Paths.get(FILE_NAME)).forEach(line -> {
            String split = line.trim();// .trim()可以去掉首尾多余的空格
            if(split.indexOf("]")>-1) {
                String uuid = split.substring(1, split.indexOf("]"));

                if (uuid.length() == 32) {
                    if (uuids.contains(uuid)) {
                        if (split.indexOf("调用方法") > -1) {
//                            System.out.println(uuid);
                            System.out.println(split);

                            map.put(split.substring(split.lastIndexOf("[")+1,split.lastIndexOf("]")),uuid);
//                            setMe.add(map);
                        }
                    }

                }
            }

        });

        System.out.println(setMe.size());
        System.out.println(map.toString());

    }

}
